package com.efast.dmom.mgmt.service.mes.report.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mgmt.bean.mes.report.QualityGateStationBean;
import com.efast.dmom.mgmt.bean.mes.report.VehicleQualityIndexBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.dao.mes.report.VehicleQualityGateDoorDataDao;
import com.efast.dmom.mgmt.service.mes.report.VehicleQualityGateDoorDataService;

@Service
public class VehicleQualityGateDoorDataServiceImpl implements VehicleQualityGateDoorDataService {

	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao;

	@Autowired
	private VehicleQualityGateDoorDataDao vehicleQualityGateDoorDataDao;

	@Override
	public void insertVehicleQualityGateDoorData(String isNow) {
		// 首先获取总装各质量门工位
		List<QualityGateStationBean> data = reportVehicleRetrospectDao.selectQualityGateStationInformation();
		// 获取不同的工艺路线id
		List<Integer> routeIdList = reportVehicleRetrospectDao.selectQualityGateStationRouteId();
		List<QualityGateStationBean> operationSortList = new ArrayList<QualityGateStationBean>();
		// 获取工艺路线上的工序及工位
		if (routeIdList.size() > 0) {
			List<QualityGateStationBean> operationList = reportVehicleRetrospectDao
					.selectOperationSortList(routeIdList);
			if (operationList != null && operationList.size() > 0) {
				for (QualityGateStationBean dataBean : operationList) {
					if (dataBean.getOperationType().equals("END")) {
						dataBean.setOperationType("NORMAL");
						QualityGateStationBean qualityData = new QualityGateStationBean();
						qualityData.setRouteId(dataBean.getRouteId());
						qualityData.setRouteName(dataBean.getRouteName());
						qualityData.setOperationId(dataBean.getNextOperationId());
						qualityData.setOperationName(dataBean.getNextOperationName());
						qualityData.setOperationType("END");
						operationSortList.add(qualityData);
					}
					operationSortList.add(dataBean);
				}
			}
		}
		// 向mybaits中插入的拼接sql
		StringBuilder dynamicRegionSql = new StringBuilder();
		// 查找每个工艺路线总装的最后一个质量门工位
		if (routeIdList != null && routeIdList.size() > 0 && operationSortList != null
				&& operationSortList.size() > 0) {
			for (Integer routeId : routeIdList) {
				for (QualityGateStationBean sortData : operationSortList) {
					if (routeId.intValue() == sortData.getRouteId() && sortData.getOperationType().equals("START")) {
						List<QualityGateStationBean> bean = new ArrayList<QualityGateStationBean>();
						// 递归排序
						List<QualityGateStationBean> indexBean = this.sortOperation(operationSortList, bean, sortData);
						boolean flag = true;
						if (indexBean != null && indexBean.size() > 0) {
							for (int i = indexBean.size() - 1; i >= 0; i--) {
								for (QualityGateStationBean routeAndOperation : data) {
									if (indexBean.get(i).getRouteId() == routeAndOperation.getRouteId() && indexBean
											.get(i).getOperationId() == routeAndOperation.getOperationId()) {
										if (dynamicRegionSql.length() > 0) {
											dynamicRegionSql.append(" or (routeId = " + routeAndOperation.getRouteId()
													+ " and staionId =" + routeAndOperation.getStaionId()
													+ " and operationId =" + routeAndOperation.getOperationId() + ")");
											flag = false;
											break;
										} else {
											dynamicRegionSql.append("(routeId = " + routeAndOperation.getRouteId()
													+ " and staionId =" + routeAndOperation.getStaionId()
													+ " and operationId =" + routeAndOperation.getOperationId() + ")");
											flag = false;
											break;
										}
									}
								}
								if (!flag) {
									break;
								}
							}
						}
					}
					continue;
				}
			}
			vehicleQualityGateDoorDataDao.deleteData(isNow);
			List<VehicleQualityIndexBean> vehicleQualityData = vehicleQualityGateDoorDataDao
					.selectVehicleDPVData(dynamicRegionSql.toString(), isNow);
			if (vehicleQualityData != null && vehicleQualityData.size() > 0) {
				for (VehicleQualityIndexBean bean : vehicleQualityData) {
					double DPV = deciMal(bean.getDayDefectQty(), bean.getDayCarQty());
					bean.setDayDPVData(DPV);
				}
				vehicleQualityGateDoorDataDao.insertAllData(vehicleQualityData);
			}
		}
	}

	public List<QualityGateStationBean> sortOperation(List<QualityGateStationBean> operationSortList,
			List<QualityGateStationBean> afterSortList, QualityGateStationBean indexBean) {

		if (indexBean != null && !"END".equals(indexBean.getOperationType())) {
			afterSortList.add(indexBean);
			for (QualityGateStationBean data : operationSortList) {
				if (indexBean.getNextOperationId() == data.getOperationId()
						&& indexBean.getRouteId() == data.getRouteId()) {
					sortOperation(operationSortList, afterSortList, data);
				}
			}
		} else {
			afterSortList.add(indexBean);
			for (QualityGateStationBean data : operationSortList) {
				if (indexBean.getNextOperationId() == data.getOperationId()
						&& indexBean.getRouteId() == data.getRouteId()) {
					afterSortList.add(data);
					return afterSortList;
				}
			}
		}
		return afterSortList;

	}

	public double deciMal(int top, int below) {
		double result = new BigDecimal((float) top / below).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return result;
	}

	@Override
	public void insertVehicleQualityFPYData(String isNow) {

		List<VehicleQualityIndexBean> dataList = vehicleQualityGateDoorDataDao.selectVehicleFPYData(isNow);
		if (dataList != null && dataList.size() > 0) {
			List<VehicleQualityIndexBean> FPYList = new ArrayList<VehicleQualityIndexBean>();
			for (VehicleQualityIndexBean data : dataList) {
				VehicleQualityIndexBean vehicleQualityIndexBean = new VehicleQualityIndexBean();
				vehicleQualityIndexBean.setCreateTime(data.getCreateTime());
				vehicleQualityIndexBean.setPlantId(data.getPlantId());
				vehicleQualityIndexBean.setCarModel(data.getCarModel());
				Calendar cal1 = Calendar.getInstance();
				cal1.setFirstDayOfWeek(Calendar.MONDAY);
				cal1.setTime(data.getCreateTime());
				double dayFPYData = 1.0;
				for (VehicleQualityIndexBean bean : dataList) {
					Calendar cal2 = Calendar.getInstance();
					cal2.setFirstDayOfWeek(Calendar.MONDAY);
					cal2.setTime(bean.getCreateTime());
					int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
					if (subYear == 0 && data.getPlantId() == bean.getPlantId()
							&& data.getCarModel().equals(bean.getCarModel())) {
						// 判断是否是同一天
						if (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
								&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)) {
							dayFPYData = dayFPYData * bean.getDayFTQData();
						}
					}
				}
				data.setDayFPYData(dayFPYData);
				FPYList.add(data);
			}
			for (int i = 0; i < FPYList.size() - 1; i++) {
				for (int j = FPYList.size() - 1; j > i; j--) {
					Calendar cal1 = Calendar.getInstance();
					cal1.setFirstDayOfWeek(Calendar.MONDAY);
					cal1.setTime(FPYList.get(i).getCreateTime());
					Calendar cal2 = Calendar.getInstance();
					cal2.setFirstDayOfWeek(Calendar.MONDAY);
					cal2.setTime(FPYList.get(j).getCreateTime());
					int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
					if (subYear == 0 && FPYList.get(j).getCarModel().equals(FPYList.get(i).getCarModel())
							&& FPYList.get(j).getPlantId() == FPYList.get(i).getPlantId()
							&& FPYList.get(j).getDayFPYData() == FPYList.get(i).getDayFPYData()) {
						if (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
								&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)) {
							FPYList.remove(j);
						}
					}
				}
			}
			vehicleQualityGateDoorDataDao.deleteAll(isNow);
			vehicleQualityGateDoorDataDao.insertData(FPYList);
		}
	}

}
